package com.aacar.op.utils.enumeration;

import com.aacar.op.utils.mapper.JsonMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;


public enum MenuPermissions {


    user("menu:user", "运营操作员管理"),
    member("menu:member", "客户管理"),
    washer("menu:washer", "洗车工管理"),
    order("menu:order", "订单管理"),
    coupon("menu:coupon", "优惠券管理");

    // 值
    private String code;

    // 名称
    private String name;

    public static final Map<String, MenuPermissions> data = Maps.newHashMap();

    static {
        for (MenuPermissions c : MenuPermissions.values()) {
            data.put(c.getCode(), c);
        }
    }

    /**
     * 根据code查找枚举类
     *
     * @param code
     */
    public static MenuPermissions parse(String code) {
        return data.get(code);
    }

    /**
     * 是否存在code对应的枚举类
     *
     * @param code
     */
    public static boolean isContain(String code) {
        boolean ret = false;
        if (StringUtils.isNotBlank(code) && data.containsKey(code)) {
            ret = true;
        }
        return ret;
    }

    private MenuPermissions(String code, String name) {
        this.code = code;
        this.name = name;
    }


    public String getCode() {
        return code;
    }


    public String getName() {
        return name;
    }

    public static String asSelect2JsonArr() {
        List<Map> ret = Lists.newArrayList();
        for (MenuPermissions m : MenuPermissions.values()) {
            Map<String, String> mapObj = Maps.newHashMap();
            mapObj.put("id", m.getCode());
            mapObj.put("text", m.getName());
            ret.add(mapObj);
        }
        return JsonMapper.alwaysMapper().toJson(ret);
    }

}
